﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.CodeDom;
using System.ComponentModel.Composition;

namespace NeuroSpeech.XamlExtensions
{
    [Export]
    public class ThicknessConverter : DefaultConverter
    {

        #region public override bool  CanProcess(XamlCodeContext context)
        public override bool CanProcess(XamlCodeContext context)
        {
            Type type = context.Peek.MemberType;
            if (type != null && type.FullName == "System.Windows.Thickness")
                return true;
            return base.CanProcess(context);
        }
        #endregion



        #region protected override CodeExpression  ConvertValue(Type type, object val)
        protected override CodeExpression ConvertValue(Type type, object val)
        {
            string strValue = val.ToString();
            string[] tokens = strValue.Split(',');
            double d = 0D;
            CodeObjectCreateExpression obj = new CodeObjectCreateExpression
            {
                CreateType = new CodeTypeReference(type)
            };
            foreach (string token in tokens)
            {
                d = double.Parse(token);
                obj.Parameters.Add(new CodePrimitiveExpression(d));
            }
            return obj;
        }
        #endregion

    }
}
